package com.example.mediastore.fragment;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.mediastore.R;
import com.example.mediastore.bean.FileBean;
import com.example.mediastore.constant.Constant;
import com.example.mediastore.utils.CommonUtil;

import java.util.List;


public class ExampleFragment extends Fragment {


    private List<FileBean> fileList;

    private ImageView imageView;
    private TextView textView;
    private RecyclerView recyclerView;

    private ProgressBar progressBar;

    @SuppressLint("MissingInflatedId")
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_example, container, false);
        showLoading(true);
        // 获取传递的文件列表
        assert getArguments() != null;
        fileList = (List<FileBean>) getArguments().getSerializable(Constant.FILE_LIST_KEY);
        //Log.d("xiaoxulu", "fileList: " + fileList);
        // 初始化 RecyclerView
        recyclerView = view.findViewById(R.id.rec_list);
        imageView=view.findViewById(R.id.iv_img);
        textView=view.findViewById(R.id.tv_text);
        if (fileList==null){
            imageView.setImageResource(R.mipmap.file);
            textView.setText(R.string.not_file);
        }

        // 设置布局管理器
        recyclerView.setLayoutManager(new LinearLayoutManager(requireContext()));

        // 其他视图初始化和操作...
        FileAdapter fileAdapter = new FileAdapter(fileList, requireContext());
        recyclerView.setAdapter(fileAdapter);
        showLoading(false);
        return view;
    }

    private void showLoading(boolean show) {
        if (progressBar != null) {
            progressBar.setVisibility(show ? View.VISIBLE : View.GONE);
        }
    }

    public  class FileAdapter extends RecyclerView.Adapter<FileAdapter.FileViewHolder> {

        private List<FileBean> fileBeanList;
        private List<FileBean> currentList;
        private Context context;

        /**
         *  this.fileBeanList=fileBeanList;
         *  this.currentList.addAll(fileBeanList) ;
         *  两种方式的区别:
         *  第一种:通过赋值的方式,两个变量指向了同一个对象,不会创建新的对象，其中一个改变会影响另一个
         *  第二种:将 list 集合中的元素逐个添加到了currentList中,两个list是不同的对象,其中一个改变并不会影响另一个
         * @param fileBeanList
         * @param context
         */
        public FileAdapter(List<FileBean> fileBeanList, Context context) {
            this.fileBeanList=fileBeanList;
            //this.currentList.addAll(fileBeanList);
            this.context = context;
        }

        @SuppressLint("NotifyDataSetChanged")
        public void refreshData(List<FileBean> fileBeanList){
            this.fileBeanList=fileBeanList;
            notifyDataSetChanged();
        }

        @NonNull
        @Override
        public FileViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(context).inflate(R.layout.item_list, parent, false);
            return new FileViewHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull FileViewHolder holder, int position) {
            FileBean fileBean = fileBeanList.get(position);
            holder.tv_name.setText(fileBean.getName());
            holder.tv_path.setText(fileBean.getPath());
            holder.iv_img.setImageResource(fileBean.getIcon());
            holder.tv_time.setText(fileBean.getTime());


            //将获取到的值传递给viewHolder
            holder.setSharePath(fileBean.getPath());
            holder.setFileName(fileBean.getName());
            holder.setLength(fileBean.getLength());
            holder.setTime(fileBean.getTime());
            holder.setCreateTime(fileBean.getCreateTime());
            holder.setUpdateTime(fileBean.getUpdateTime());
        }

        @Override
        public int getItemCount() {
            return fileBeanList == null ? 0 : fileBeanList.size();
        }

        class FileViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

            TextView tv_name, tv_path, tv_time;
            ImageView iv_img, btn_detail;
            RelativeLayout relativeLayout;
            LinearLayout linearLayout;


            private String sharePath, fileName, length, time, createTime, updateTime; // 添加sharePath字段

            public void setSharePath(String sharePath) {
                this.sharePath = sharePath;
            }

            public void setFileName(String fileName) {
                this.fileName = fileName;
            }

            public void setLength(String length) {
                this.length = length;
            }

            public void setTime(String time) {
                this.time = time;
            }

            public void setCreateTime(String createTime) {
                this.createTime = createTime;
            }

            public void setUpdateTime(String updateTime) {
                this.updateTime = updateTime;
            }

            public FileViewHolder(@NonNull View itemView) {
                super(itemView);
                tv_name = itemView.findViewById(R.id.tv_name);
                tv_path = itemView.findViewById(R.id.tv_path);
                tv_time = itemView.findViewById(R.id.tv_time);
                iv_img = itemView.findViewById(R.id.iv_img);
                btn_detail = itemView.findViewById(R.id.btn_detail);
                relativeLayout = itemView.findViewById(R.id.rl_container);
                linearLayout = itemView.findViewById(R.id.begin_audio);



                btn_detail.setOnClickListener(this);
                linearLayout.setOnClickListener(this);

                linearLayout.setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View v) {
                        CommonUtil.shareFile(sharePath, requireContext());
                        return true;
                    }
                });
            }

            @Override
            public void onClick(View v) {

                int position = getAdapterPosition();
                if (position != RecyclerView.NO_POSITION) {
                    if (v.getId() == R.id.btn_detail) {
                        CommonUtil.openDialog(iv_img,createTime,updateTime,length,tv_name,tv_path);
                    } else if (v.getId() == R.id.begin_audio) {
                        CommonUtil.openFile(requireContext(), sharePath, fileName, time);
                    }
                }
            }



        }
    }
}